package com.weizhu.swufer.core.filter;

import com.weizhu.swufer.constant.SwuferConstant;
import com.weizhu.swufer.core.annotation.SwuferFilter;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.rpc.RpcContext;
import com.weizhu.swufer.core.rpc.RpcRequest;

import java.util.HashMap;
import java.util.Map;

@SwuferFilter(type = SwuferConstant.FILTER_TYPE_CLIENT,order = Integer.MAX_VALUE)
public class ClientContextFilter implements Filter{
    @Override
    public Object doFilter(InvokeParam invokeParam, FilterChain filterChain) throws Exception {
        RpcRequest rpcRequest = invokeParam.getRpcRequest();
        Map<String, Object> contextParameters = rpcRequest.getContextParameters();
        if (contextParameters == null){
            contextParameters = new HashMap<>();
            rpcRequest.setContextParameters(contextParameters);
        }
        contextParameters.putAll(RpcContext.getContext().getContextParameters());
        return filterChain.doFilter(invokeParam);
    }
}
